// mircoApp/assessment/assessment_questionnaire_record/assessment_questionnaire_record.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadMore: true,
    page: 1,
    total_page: 0,
    info: [],
    show: false,
    resultDetail: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      options: options,
      imgHost: `${app.globalData.imgPath}/assessment/assessment_icon_1.png`
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.getData();
  },
  init() {
    this.setData({
      isLoadMore: true,
      page: 1,
      total_page: 0,
      info: [],
      show: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoadMore){
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  watchReport(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (item.status == 0) {
      this.resultPost(item.id);
    } else {
      wx.navigateTo({
        url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${item.id}&view=true`,
      })
    }
  },
  Unlock() {
    if (this.data.resultDetail.is_pay == 0 && this.data.resultDetail.rest_count > 0) {
      app._getNetWork({
        url: app.getNetAddresss("plugin.assessment.frontend.question.freeResult"),
        data: {
          id: this.data.resultDetail.id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            wx.navigateTo({
              url: `/mircoApp/assessment/assessmentReport/assessmentReport?id=${this.data.resultDetail.id}&view=true`,
            })
          } else {
            app.tips(res.msg);
          }
        }
      });
    } else {
      app._getNetWork({
        url: app.getNetAddresss("plugin.assessment.frontend.create.index"),
        data: {
          ass_log_id: this.data.resultDetail.id
        },
        success: (resdata) => {
          let res = resdata.data;
          if (res.result == 1) {
            wx.navigateTo({
              url: `/packageD/buy/orderPay/orderPay?order_id=${res.data.order_ids}&ass_log_id=${this.data.resultDetail.id}&view=record&plugName=orderpay_assessment`,
            })
          } else {
            app.tips(res.msg);
          }
        }
      });
    }
  },
  cancelTap() {
    this.setData({
      show: false
    })
  },
  resultPost(rid) {
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.resultDetail"),
      data: {
        id: rid
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          console.log(res.data)
          this.setData({
            show: true,
            resultDetail: res.data
          })
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  watchQuestion(e) {
    let {
      item
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/mircoApp/assessment/assessment_questionnaire/assessment_questionnaire?id=${item.id}&view=true`,
    })
  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.resultLog"),
      data: {
        page: this.data.page
      },
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.setData({
            info: res.data.data,
            isLoadMore: true,
            total_page: res.data.last_page,
          })
          if (!res.data.last_page) {
            this.setData({
              total_page: 0
            })
          }
        } else {
          app.tips(res.msg);
        }
      }
    });
  },
  getMoreData() {
    if(this.data.page >= this.data.total_page) return
    wx.showLoading({
      title: '加载更多',
    })
    this.setData({
      page: this.data.page + 1
    })
    app._getNetWork({
      url: app.getNetAddresss("plugin.assessment.frontend.question.resultLog"),
      data: {
        page: this.data.page
      },
      success: (resdata) => {
        wx.hideLoading()
        let res = resdata.data;
        if (res.result == 1) {
          this.data.info = this.data.info.concat(res.data.data);
          this.setData({
            info: this.data.info
          })
        } else {
          app.tips(res.msg);
        }
      },
      fail:(res)=>{
        wx.hideLoading()
      }
    });
  }
})